package com.chd.driversystem.util.tools;

import android.util.Log;

import com.chd.driversystem.domain.DrivingRecord;
import com.chd.driversystem.domain.Oil;
import com.chd.driversystem.domain.Operation;
import com.chd.driversystem.gen.BeanServer;
import com.chd.driversystem.util.network.bean.DayData;
import com.chd.driversystem.util.network.bean.MonthData;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by feiyu on 17-6-7.
 */

public class ResolveNetData {

    private static final String TAG = "ResolveNetData";

    private static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
    private static DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM");

    public static int resolveDateData(DayData dayData, String driverId) throws ParseException {
        int count = 0;
        Long id = BeanServer.getInstance().getUserByCode(driverId).getId();
        List<DrivingRecord> drivingRecords = new ArrayList<>();
        for (DayData.DayMileBean mile :
                dayData.getDayMile()) {
            DrivingRecord drivingRecord = new DrivingRecord(
                    null,
                    id,
                    dateFormat.parse(mile.getRecordDate()),
                    mile.getMileDay(),
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0
            );
            drivingRecords.add(drivingRecord);
        }
        count += BeanServer.getInstance().saveDrivingRecord(drivingRecords);

        List<Oil> oils = new ArrayList<>();
        for (DayData.DayOilBean netOil :
                dayData.getDayOil()) {
            Oil oil = new Oil(
                    null,
                    id,
                    dateFormat.parse(netOil.getRecordDate()),
                    (double) netOil.getOilDay(),
                    0.0,
                    0.0,
                    0.0
            );
            oils.add(oil);
        }
        count += BeanServer.getInstance().saveOilList(oils);

        int size = dayData.getDaySwitchBreak().size();
        List<Operation> operations = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            DayData.DaySwitchBreakBean shiftBreak = dayData.getDaySwitchBreak().get(i);
            DayData.DayAccDecBean accDec = dayData.getDayAccDec().get(i);
            Operation operation = new Operation(
                    null,
                    id,
                    dateFormat.parse(shiftBreak.getRecordDate()),
                    shiftBreak.getBreakTimes(),
                    shiftBreak.getSwitchTimes(),
                    accDec.getAccTimes(),
                    accDec.getDecTimes(),
                    0
            );
            operations.add(operation);
        }
        count += BeanServer.getInstance().saveOperations(operations);
        return count;
    }

    public static int resolveMonthData(MonthData monthData, String driverId) throws ParseException {
        int count = 0;
        Long id = BeanServer.getInstance().getUserByCode(driverId).getId();
        List<DrivingRecord> drivingRecords = new ArrayList<>();
        for (MonthData.MonthMileBean mile :
                monthData.getMonthMile()) {
            DrivingRecord drivingRecord = new DrivingRecord(
                    null,
                    id,
                    dateFormat1.parse(mile.getRecordDate()),
                    0,
                    mile.getMileMonth(),
                    0,
                    0,
                    0,
                    0,
                    0,
                    0
            );
            drivingRecords.add(drivingRecord);
        }
        count += BeanServer.getInstance().saveDrivingRecord(drivingRecords);

        List<Oil> oils = new ArrayList<>();
        for (MonthData.MonthOilBean netOil :
                monthData.getMonthOil()) {
            Oil oil = new Oil(
                    null,
                    id,
                    dateFormat1.parse(netOil.getRecordDate()),
                    0.0,
                    0.0,
                    (double) netOil.getOilMonth(),
                    0.0
            );
            oils.add(oil);
        }
        count += BeanServer.getInstance().saveOilList(oils);
        return count;
    }
}
